package com.taotao.taodada.manager;

import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.taotao.taodada.common.ErrorCode;
import com.taotao.taodada.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author： HTTT
 * @Date： 2024/6/15 18:26
 * @Describe：
 */
@Component
public class QWAiManager {

    @Value("${aikey.qw}")
    private String apiKey;

    public String doRequest(String systemMessage,String userMessage){
        Generation gen = new Generation();
        List<Message> messages = new ArrayList<>();
        Message systemMsg =
                Message.builder().role(Role.SYSTEM.getValue()).content(systemMessage).build();
        Message userMsg = Message.builder().role(Role.USER.getValue()).content(userMessage).build();
        messages.add(systemMsg);
        messages.add(userMsg);
        GenerationParam param = GenerationParam.builder().model(Generation.Models.QWEN_TURBO).messages(messages)
                .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                .apiKey(apiKey)
                .build();
        try {
            GenerationResult result = gen.call(param);
            return result.getOutput().getChoices().get(0).getMessage().getContent();
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, e.getMessage());
        }
    }
}
